import _ from 'lodash'
import {
  xAxisConfig,
  yAxisConfig,
  lineSeriesConfig,
  barSeriesConfig,
  pictorialBarSeriesConfig
} from '../common/chartsConfig'

export function getxAxisConfig(config){
  const { xAxisData } = config
  const xAxisCopy = _.cloneDeep(xAxisConfig)
  xAxisCopy.data = xAxisData
  return xAxisCopy
}

export function getyAxisConfig(config){
  const { yAxisType } = config
  let yAxisCopy01 = _.cloneDeep(yAxisConfig)
  let yAxisCopy02 = _.cloneDeep(yAxisConfig)
  yAxisCopy02 = _.merge(yAxisCopy02,{
    axisLabel:{
      formatter:"{value}%"
    }
  })
  if(yAxisType==="double"){
    return [yAxisCopy01,yAxisCopy02]
  }else{
    return [yAxisCopy01]
  }
}

export function getSeriesConfig(config){
  const { data,barWidth } = config
  let series = []
  data.forEach(item=>{
    if(item.type==="line"){
      let seriesCopy = _.cloneDeep(lineSeriesConfig)
      seriesCopy = _.merge(seriesCopy, {
        name:item.name,
        data:item.data
      })
      if(item.area){
        seriesCopy = _.merge(seriesCopy, {
          areaStyle:{
            opacity: 1,
            color:item.areaColor
          }
        })
      }
      if(item.axisIndex){
        seriesCopy = _.merge(seriesCopy, {
          yAxisIndex:item.axisIndex
        })
      }
      
      series.push(seriesCopy)
    }
    if(item.type==="bar"){
      let seriesCopy = _.cloneDeep(barSeriesConfig)
      seriesCopy = _.merge(seriesCopy, {
        name:item.name,
        data:item.data
      })
      if(item.stack){
        seriesCopy = _.merge(seriesCopy, {
          stack:item.stack
        })
      }
      if(barWidth){
        seriesCopy = _.merge(seriesCopy, {
          barWidth:barWidth
        })
      }
      if(item.borderRadius===null){
        seriesCopy = _.merge(seriesCopy, {
          itemStyle:{
            borderRadius:0
          },
        })
      }
      series.push(seriesCopy)
    }
    if(item.type==="pictorialBar"){
      let seriesCopy = _.cloneDeep(pictorialBarSeriesConfig)
      seriesCopy = _.merge(seriesCopy, {
        name:item.name,
        data:item.data
      })
      series.push(seriesCopy)
    }
  })
  return series
}